/**
 * Created by yys on 2016/11/21.
 */
$(function () {
    $('#image').ace_file_input({
        no_file: '还未选择图片',
        btn_choose: '选择',
        btn_change: '重新选择',
        droppable: false,
        onchange: null,
        thumbnail: true //| true | large
    });
    $("#price").change(function(){
        var price= $(this).val();
        console.log('金额在改变：'+price);
        if(!checkDecimals(price)){
            swal({
                title: "格式不正确!!！",
                text: "",
                type: "error",
                timer:1000
            });
            $(this).val(0);
        }
    });
});
$('#tag_search').click(function () {
    var keyword=$("#tag_search_keyword").val();
    keyword=trim(keyword);
    $.ajax({
        type: "POST",
        url: "/backstage/tag/search-tag",
        data: {'search':keyword,'app_id':1,'type':1},
        success: function (msg) {
            if(msg.state=1){
                var html='';
                for(var a=0;a<msg['data'].length;a++) {
                    html+='<option value="' + msg['data'][a].id + '">'+ msg['data'][a].name +'(Id:'+ msg['data'][a].id +')</option><br />';
                }
                $("#tag_search_res").html(html);
            }
        }, error: function () {
            swal({title: "系统繁忙！", text: "",type: "error", timer:1000 })
        }
    });
});
function addSourcePicTag()
{
    var selText=$("#tag_search_res option:selected").text();
    var selVal=$("#tag_search_res option:selected").val();
    var flag=true;
    if((selText=='')||(typeof selVal=='undefined')){
        return false;
    }
    if((selText=='')||(typeof selVal=='undefined')){
        return false;
    }
    $("#selected-tag-div input").each(function (){
        var val = $(this).val();
        if(val==selVal){
            flag=false;
        }
    });
    if(flag){
        $('#selected-tag-div').append('<input name="p_tag_ids[]" type="checkbox" value="'+selVal+'" checked="checked" />'+selText+'</br>');
    }
}

/**
 * 产品模板分类搜索
 */
$('#prod_temp_search').click(function () {
    var keyword=$("#prod_temp_keyword").val();
    keyword=trim(keyword);
    $.ajax({
        type: "POST",
        url: "/backstage/product-temp/search-temp",
        data: {'search':keyword},
        success: function (msg) {
            console.log(msg);
            if(msg.state=1){
                var html='';
                for(var a=0;a<msg['data'].length;a++) {
                    html+='<option value="' + msg['data'][a].id + '">'+ msg['data'][a].name +'</option><br />';
                }
                $("#prod_temp_category_search_res").html(html);
            }
        }, error: function () {
            swal({title: "系统繁忙！", text: "",type: "error", timer:1000 })
        }
    });
});
/**
 * 去除字符串前后空格
 * */
function trim(s){
    return s.replace(/(^\s*)|(\s*$)/g, "");
}
/***
 * 添加品牌
 */
function addProdTemp()
{
    var selText=$("#prod_temp_search_res option:selected").text();
    var selVal=$("#prod_temp_search_res option:selected").val();
    if(!empty(selText)){
        $('#brand_name').val(selText);
    }
    if(!empty(selVal)){
        $('#brand_id').val(selVal);
    }
}
/***
 *
 * @param options
 * @param index
 */
function resultChange(options,index) {
    for(var i=0; i<options.length; i++){
        if(i==index){
            options[i].selected = true;
        }else{
            options[i].selected = false;
        }
    }
}
/***
 * 判断字符串是不是为空
 * @param exp
 * @returns {boolean}
 */
function empty(exp) {
    exp=trim(exp);
    if (!exp || typeof(exp)=="undefined" && exp=='')
    {
        return true;
    }
    return false;
}
/***
 * 小数格式校验
 * @param num
 * @returns {boolean}
 */
function checkDecimals(num)
{
    var reg = /^\d+(?=\.{0,1}\d+$|$)/
    return reg.test(num);
}